1 /************************************************************
2 * Copyright *
3 * Portions of this software are Copyright (c) 1993 - 2002, *
4 * Chad Z. Hower (Kudzu) and the Indy Pit Crew *
5 * - http://www.nevrona.com/Indy/ *
6 ************************************************************/
7 package org.indy;
8
9 import java.util.Collection;
10 import java.util.HashSet;
11 import java.util.Iterator;
12
13 import org.indy.util.IndyUtilities;
14
15
16 /***
17 * Base class for Indy connection interceptos.
18 *
19 *@author OTG
20 *@version 0.1
21 */
22 public class ConnectionIntercept {
23 // static ResourceBundle res = ResourceBundle.getBundle("org.indy.tcp.Res");
24 private IndyComponent connection;
25 private ConnectionIntercept intercept;
26 private boolean client = true;
27 private Collection listeners = new HashSet();
28
29 /***
30 * Constructs a new IdConnectionIntercept instance
31 */
32 public ConnectionIntercept() {
33 super();
34 }
35
36 /***
37 * Sets the intercept attribute of the IdConnectionIntercept object
38 *
39 *@param i The new intercept value
40 *@throws IdInterceptCircularLinkException Description of the Exception
41 */
42 protected void setIntercept(ConnectionIntercept i)
43 throws InterceptCircularLinkException {
44 ConnectionIntercept incpt = i;
45
46 while (incpt != null) {
47 //recurse through intercepts
48 if (incpt == this) {
49 //comparing refs here NOT equivalence (hence ==, not .equals())
50 throw new InterceptCircularLinkException(IndyUtilities.getResourceString(
51 "RSInterceptCircularLink"));
52 }
53
54 incpt = incpt.getIntercept();
55 }
56
57 intercept = i;
58 }
59
60 /***
61 * Sets the isClient attribute of the IdConnectionIntercept object
62 *
63 *@param isClient The new isClient value
64 */
65 public void setIsClient(boolean isClient) {
66 client = isClient;
67 }
68
69 /***
70 * Gets the intercept attribute of the IdConnectionIntercept object
71 *
72 *@return The intercept value
73 */
74 protected ConnectionIntercept getIntercept() {
75 return intercept;
76 }
77
78 /***
79 * Gets the client attribute of the IdConnectionIntercept object
80 *
81 *@return The client value
82 */
83 public boolean isClient() {
84 return client;
85 }
86
87 private void doOnConnect() {
88 Iterator i = listeners.iterator();
89
90 while (i.hasNext()) {
91 ((ConnectionInterceptListener) i.next()).onConnect(this);
92 }
93 }
94
95 private void doOnDisconnect() {
96 Iterator i = listeners.iterator();
97
98 while (i.hasNext()) {
99 ((ConnectionInterceptListener) i.next()).onDisconnect(this);
100 }
101 }
102
103 private void doOnRead(byte[] b) {
104 Iterator i = listeners.iterator();
105
106 while (i.hasNext()) {
107 ((ConnectionInterceptListener) i.next()).onRead(this, b);
108 }
109 }
110
111 private void doOnWrite(byte[] b) {
112 Iterator i = listeners.iterator();
113
114 while (i.hasNext()) {
115 ((ConnectionInterceptListener) i.next()).onWrite(this, b);
116 }
117 }
118
119 /***
120 * Description of the Method
121 *
122 *@param connection Description of the Parameter
123 */
124 protected void nestedConnect(IndyComponent connection) {
125 if (intercept != null) {
126 intercept.connect(connection);
127 }
128 }
129
130 /***
131 * Description of the Method
132 */
133 protected void nestedDisconnect() {
134 if (intercept != null) {
135 intercept.disconnect();
136 }
137 }
138
139 /***
140 * Description of the Method
141 *
142 *@param b Description of the Parameter
143 */
144 protected void nestedRead(byte[] b) {
145 if (intercept != null) {
146 intercept.read(b);
147 }
148 }
149
150 /***
151 * Description of the Method
152 *
153 *@param b Description of the Parameter
154 */
155 protected void nestedWrite(byte[] b) {
156 if (intercept != null) {
157 intercept.write(b);
158 }
159 }
160
161 /***
162 * Adds a feature to the ConnectionInterceptListener attribute of the
163 * IdConnectionIntercept object
164 *
165 *@param l The feature to be added to the ConnectionInterceptListener
166 * attribute
167 */
168 public void addConnectionInterceptListener(ConnectionInterceptListener l) {
169 listeners.add(l);
170 }
171
172 /***
173 * Description of the Method
174 *
175 *@param l Description of the Parameter
176 */
177 public void removeConnectionInterceptListener(ConnectionInterceptListener l) {
178 listeners.remove(l);
179 }
180
181 /***
182 * Description of the Method
183 *
184 *@param b Description of the Parameter
185 */
186 public void read(byte[] b) {
187 nestedRead(b);
188 doOnRead(b);
189 }
190
191 /***
192 * Description of the Method
193 *
194 *@param b Description of the Parameter
195 */
196 public void write(byte[] b) {
197 nestedWrite(b);
198 doOnWrite(b);
199 }
200
201 /***
202 * Description of the Method
203 *
204 *@param comp Description of the Parameter
205 */
206 public void connect(IndyComponent comp) {
207 connection = comp;
208 doOnConnect();
209 nestedConnect(comp);
210 }
211
212 /***
213 * Description of the Method
214 */
215 public void disconnect() {
216 nestedDisconnect();
217 doOnDisconnect();
218 connection = null;
219 }
220 }
This page was automatically generated by Maven